package com.actionpals.components.controls
{
	import flash.events.FocusEvent;
	
	public class DefaultTextInput extends TextInput
	{
		protected var _defaultText:String;
		protected var _isDefault:Boolean;
		protected var _displayAsPassword:Boolean;
		
		public function DefaultTextInput( defaultText:String="" )
		{
			this.defaultText = defaultText;
			
			super();
		}
		
		public override function get displayAsPassword():Boolean
		{
			return _displayAsPassword;
		}
		public override function set displayAsPassword( value:Boolean ):void
		{
			_displayAsPassword = value;
		}
		
		public override function set text( value:String ):void
		{
			field.text = value;
			
			if( displayAsPassword )
			{
				super.displayAsPassword = !isDefault;
			}
		}
		
		public function get defaultText():String
		{
			return _defaultText;
		}
		public function set defaultText( value:String ):void
		{
			_defaultText = value;
		}
		
		public function get isDefault():Boolean
		{
			return field.text == defaultText;
		}
		
		protected override function init():void
		{
			super.init();
			field.text = defaultText;
		}
		
		
		protected override function focusInHandler( evt:FocusEvent ):void
		{
			if( field.text == defaultText ) field.text = "";
			if( displayAsPassword ) super.displayAsPassword = true;
			
			super.focusInHandler( evt );
		}
		
		protected override function focusOutHandler( evt:FocusEvent ):void
		{
			if( field.text == "" )
			{
				field.text = defaultText;
				if( displayAsPassword && defaultText != "" )
				{
					super.displayAsPassword = false;
				}
			}
		}
		
	}
}